<?php

class FormsController extends Zend_Controller_Action {

    public function preDispatch() {
        $t = Zend_Registry::get('Zend_Locale');
        $this->view->navigation_line = array($t->_('Forms') => ADMIN_URL . '/forms');
        $session = new Zend_Session_Namespace('UnCMS');
        if (!isset($session->acl->forms))
            $this->_redirect(SITE_URL);
    }

    public function indexAction() {
        $model = new FormsModel();
        $this->view->list = $model->getForms();
    }

    public function editAction() {
        $forms = new FormsForm();
        $fid = (int) $this->getRequest()->getParam('id', 0);
        $t = Zend_Registry::get('Zend_Locale');
        $form = $forms->Forms($fid);
        $this->view->form = $form;
        $model = new FormsModel();
        if ($fid > 0) {
            $formdata = $model->getForms($fid);
            $fields = $model->getFields($fid);
            $this->view->formdata = $formdata;
            $this->view->fields = $fields;
            $this->view->navigation_line = $this->view->navigation_line + array($formdata['title'] => ADMIN_URL . '/forms/edit/id/' . $fid);
        } else {
            $this->view->navigation_line = $this->view->navigation_line + array($t->_('New form') => ADMIN_URL . '/snippet/edit/id/0');
        }

        if ($this->_request->isPost()) {
            $Data = $this->_request->getPost();
            if ($form->isValid($Data)) {
                array_pop($Data);
                $fid = $model->updateForm($Data, $fid);
                $this->_redirect('/forms/edit/id/' . $fid);
            } else {
                $form->populate($Data);
            }
        }
        if ($fid > 0) {
            $form->populate($formdata);
        }
    }

    public function deletefieldAction() {
        $model = new FormsModel();
        $id = (int) $this->getRequest()->getParam('id', 0);
        $fid = (int) $this->getRequest()->getParam('fid', 0);
        $model->deleteField($id);
        $this->_redirect('/forms/edit/id/' . $fid);
    }

    public function fieldAction() {
        $forms = new FormsForm();
        $model = new FormsModel();
        $t = Zend_Registry::get('Zend_Locale');
        $id = (int) $this->getRequest()->getParam('id', 0);
        $fid = (int) $this->getRequest()->getParam('fid', 0);
        $form = $forms->Field($fid, $id);
        $this->view->form = $form;
        $formdata = $model->getForms($fid);

        $this->view->formdata = $formdata;

        if ($id > 0) {
            $fielddata = $model->getFields($fid, $id);
            $this->view->fielddata = $fielddata;
            $this->view->navigation_line = $this->view->navigation_line + array($formdata['title'] => ADMIN_URL . '/forms/edit/id/' . $fid, $fielddata['title'] => ADMIN_URL . '/forms/edit/id/' . $id . '/fid/' . $fid);
        } else {
            $this->view->navigation_line = $this->view->navigation_line + array($formdata['title'] => ADMIN_URL . '/forms/edit/id/' . $fid, $t->_('New field') => ADMIN_URL . '/forms/field/id/0');
        }

        if ($this->_request->isPost()) {
            $Data = $this->_request->getPost();
            if ($form->isValid($Data)) {
                unset ($Data['submit']);
                $Data['fid'] = $fid;
                $model->updateField($Data, $id);
                $this->_redirect('/forms/edit/id/' . $fid);
            } else {
                $form->populate($Data);
            }
        }
        if ($id > 0) {
            $form->populate($fielddata);
        }
    }

    public function deleteAction() {
        $model = new FormsModel();
        $id = (int) $this->getRequest()->getParam('id', 0);
        $model->deleteForm($id);
        $this->_redirect('/forms');
    }
    
    public function tagsAction() {
        $filter = new Zend_Filter_Alnum();
        $object = mb_strtolower(trim($filter->filter($this->getRequest()->getParam('object', ''))));

        $model = new TagsModel();
        $q = mb_strtolower($_GET["term"]);
        if (!$q)
            exit;

        $items = array();
        $tags = $model->getTags($object);
        $i = 0;
        foreach ($tags as $k => $v) {
            if (strpos(mb_strtolower($v), $q) !== false) {
                $results[$i]['id'] = $k;
                $results[$i]['label'] = $v;
                $results[$i]['value'] = $v;
                $i++;
            }
        }
        if (isset($results))
            print json_encode($results);
        exit;
    }

}
